Introduction Unit aims, objectives and prerequisites. | ||
Organization of Sequential files Provides a recap of Sequential file organization and the difficulties of adding, removing and updating records in an ordered Sequential file. | ||
Organization
of Relative files Describes how Relative files are organized and shows how records may be added, deleted or updated. | ||
Organization
of Indexed files Describes the organization of Indexed files. Shows how records may be added, deleted or updated and describes how records in an Indexed file may be processed directly or sequentially on any key. | ||
Comparison
of COBOL file organizations
|
|
Sequential files | |||
Introduction |
Access to records in a Sequential file is serial. To reach a particular record, all the preceding records must be read. As we observed when the topic was introduced earlier in the course, the organization of an unordered Sequential file means it is only practical to read records from the file and add records to the end of the file (OPEN..EXTEND). It is not practical to delete or update records. While it is possible to delete, update and insert records in an ordered Sequential file, these operations have some drawbacks. | ||
Problems accessing ordered Sequential files. |
Records in an ordered Sequential file are arranged, in order, on some key field or fields. When we want to insert,delete or amend a record we must preserve the ordering. The only way to do this is to create a new file. In the case of an insertion or update, the new file will contain the inserted or updated record. In the case of a deletion, the deleted record will be missing from the new file. The main drawback to inserting, deleting or amending records in an ordered Sequential file is that the entire file must be read and then the records written to a new file. Since disk access is one of the slowest things we can do in computing this is very wasteful of computer time when only a few records are involved. For instance, if 10 records are to be inserted into a 10,000 record file, then 10,000 records will have to be read from the old file and 10,010 written to the new file. The average time to insert a new record will thus be very great.
| ||
Contrast with direct access files |
While Sequential files have a number of advantages over other types of file organization (and these are discussed fully in the final section) the fact that a new file must be created when we delete, update or insert a records causes problems. These problems are addressed by direct access files. Direct access files allow us to read, update, delete and insert individual records, in situ, using a key. | ||
Inserting records in an ordered Sequential file |
To insert a record in an ordered Sequential file:
| ||
Deleting records from an ordered Sequential file |
To delete a record in an ordered Sequential file:
| ||
Amending records in an ordered Sequential file |
To amend a record in an ordered Sequential file:
| ||
Sequential files - animation |
| ||
Summary |
The problem with Sequential files is that unless the file is ordered very few (only read and add) operations can be applied to it. Even when a Sequential file is ordered; delete, insert and amend operations are prohibitively expensive (in processing terms) when only a few records in the file are affected (i.e. when the "hit rate" is low) .
|
|
|
Indexed Files | |||
Introduction |
While the usefulness of a Relative file is constrained by its restrictive key, Indexed files suffer from no such limitation. Indexed files may have up to 255 keys, the keys can be alphanumeric and only the primary key must be unique. In addition, it is possible to read an Indexed file sequentially on any of its keys. | ||
Organization of Indexed files |
An Indexed file may have multiple keys. The key upon which the data records are ordered is called the primary key. The other keys are called alternate keys. Records in the Indexed file are sequenced on ascending primary key. Over the actual data records, the file system builds an index. When direct access is required, the file system uses this index to find, read, insert, update or delete, the required record.
For each of the alternate keys specified in an Indexed file, an alternate index is built. However, the lowest level of an alternate index does not contain actual data records. Instead, this level made up of base records which contain only the alternate key value and a pointer to where the actual record is. These base records are organized in ascending alternate key order. As well as allowing direct access to records on the primary key or any of the 254 alternate keys, indexed files may also be processed sequentially. When processed sequentially, the records may be read in ascending order on the primary key or on any of the alternate keys. Since the data records are in held in ascending primary key sequence it is easy to see how the file may be accessed sequentially on the primary key. It is not quite so obvious how sequential on the alternate keys is achieved. This is covered in the unit on Indexed files. | ||
Animation - Organization and use of Indexed files |
| ||
Summary |
An Indexed file may have multiple, alphanumeric, keys. Only the primary key must be unique. For each key specified for an Indexed file, an index will be built. |
|
Comparison of COBOL file organizations | |||||||||||||||||||
Introduction |
In this section we examine the advantages and disadvantages of the three COBOL file organizations. | ||||||||||||||||||
Sequential files |
Disadvantages
Advantages
| ||||||||||||||||||
Relative files |
Disadvantages
Advantages
| ||||||||||||||||||
Indexed files |
Disadvantages
Advantages
| ||||||||||||||||||
Summary |
There is no one best file organization. Choosing an appropriate file organization is a case of "horses for courses". If the hit rate is high and we have no need for direct access to the records, a Sequential file might be best. If the hit rate is low or if we require direct access to the records but one numeric key is sufficient, a Relative file might be the best choice. If we need direct access on a number of keys or if the key must be alphanumeric, then we must choose an Indexed file. |
|
Copyright NoticeThese COBOL course materials are the copyright property of Michael Coughlan. All rights reserved. No part of these course materials may be reproduced in any form or by any means - graphic, electronic, mechanical, photocopying, printing, recording, taping or stored in an information storage and retrieval system - without the written permission of the author. (c) Michael Coughlan Last updated : April 1998 |